package com.wswy.wzcx.ui.persenter;

import com.che.libcommon.utils.RxBus;
import com.wswy.wzcx.model.license.UserCarInfo;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.ui.view.MyCarCollectView;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.ResourceObserver;

/**
 * Created by wangduheng26 on 2018/1/31.
 * wangduheng26@gmail.com
 */

public class MyCarCollectPresenter {
    private MyCarCollectView mView;
    protected CompositeDisposable disposables;

    public MyCarCollectPresenter(MyCarCollectView view) {
        this.mView = view;
    }

    public void onCreate() {
        disposables = new CompositeDisposable();
        ResourceObserver<UserCarInfo> updateObserver = new ResourceObserver<UserCarInfo>() {
            @Override
            public void onNext(UserCarInfo userCarInfo) {
                mView.updateItem(userCarInfo);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        ResourceObserver<UserCarInfo> observer = new ResourceObserver<UserCarInfo>() {
            @Override
            public void onNext(UserCarInfo userCarInfo) {
                mView.deleteItem(userCarInfo.id);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        disposables.add(observer);
        disposables.add(updateObserver);
        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_DELETE,
                UserCarInfo.class)
                .subscribe(observer);
        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_ADD_UPDATE,
            UserCarInfo.class)
            .subscribe(updateObserver);
    }

    public void onDestroy(){
        if(disposables!=null){
            disposables.dispose();
        }
    }
}
